<?php

namespace app\models\user;

use app\components\Fun;
use app\components\Lcms;
use Yii;

/**
 * This is the model class for table "{{%identification_list}}".
 *
 * @property int $id
 * @property int|null $time 创建时间
 * @property string|null $identification_password 生成的唯一的随机字符串,用于查询用户的真id
 * @property int|null $uid 用户的id
 */
class IdentificationList extends \app\models\BaseUserModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%identification_list}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['time', 'uid'], 'integer'],
            [['identification_password'], 'string', 'max' => 255],
            [['identification_password'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'time' => 'Time',
            'identification_password' => 'Identification',
            'uid' => 'Uid',
        ];
    }




    /**
     * 创建一个用于鉴定身份的id密码
    */
    public static function createIdentification($uid){
        self::deleteAll('time<:time OR uid=:uid',[
            ':time'=>time()-60*60*24*7,
            ':uid'=>$uid
        ]);
        $new=new self;
        $new->identification_password=Fun::uniqid($uid);
        $new->uid=$uid;
        $new->time=time();
        $new->save();
        return $new->identification_password;
    }



    /**
     * 根据鉴别密码获取用户uid
    */
    public static function getUid($identification_password){
        $one=self::find()->where([
            'identification_password'=>$identification_password
        ])->one();
        if(!$one){
            return false;
        }
       // $one->delete();
        if($one->time<(time()-60*60*24*7)){
            $one->delete();
            return false;
        }
       //// var_dump($one->uid);
      //  $one->delete();
       // var_dump($one->uid);exit;
        return $one->uid;
    }





}
